import { config } from '@vue/test-utils';
import { vi } from 'vitest';

// 模拟 PrimeVue 组件
config.global.stubs = {
  Toast: true,
  Dialog: true,
  Button: true,
  DataTable: true,
  Column: true,
  InputText: true,
  Password: true,
  Dropdown: true,
  Calendar: true,
  Checkbox: true,
  RadioButton: true,
  Textarea: true,
  ProgressSpinner: true,
  ProgressBar: true,
  Message: true,
  Sidebar: true,
  Menu: true,
  Menubar: true,
  TabView: true,
  TabPanel: true,
  Accordion: true,
  AccordionTab: true,
  Card: true,
  Panel: true,
  Divider: true,
  Badge: true,
  Tag: true,
  Chip: true,
  Avatar: true,
  Image: true,
  Galleria: true,
  Carousel: true,
  FileUpload: true,
  Rating: true,
  Slider: true,
  ToggleButton: true,
  SelectButton: true,
  MultiSelect: true,
  AutoComplete: true,
  TreeSelect: true,
  CascadeSelect: true,
  Chart: true,
  Tree: true,
  TreeTable: true,
  Timeline: true,
  OrganizationChart: true,
  Paginator: true,
  VirtualScroller: true,
  ScrollPanel: true,
  Splitter: true,
  SplitterPanel: true,
  Toolbar: true,
  ConfirmDialog: true,
  ConfirmPopup: true,
  OverlayPanel: true,
  Tooltip: true,
  SpeedDial: true,
  ScrollTop: true,
  Skeleton: true,
  ProgressSpinner: true,
  Ripple: true
};

// 模拟全局属性
config.global.mocks = {
  $toast: {
    add: vi.fn()
  },
  $confirm: {
    require: vi.fn()
  }
};

// 模拟路由
const mockRouter = {
  push: vi.fn(),
  replace: vi.fn(),
  go: vi.fn(),
  back: vi.fn(),
  forward: vi.fn(),
  resolve: vi.fn(() => ({ href: '/' })),
  currentRoute: {
    value: {
      path: '/',
      query: {},
      params: {},
      meta: {}
    }
  }
};

const mockRoute = {
  path: '/',
  query: {},
  params: {},
  meta: {},
  name: 'home',
  fullPath: '/',
  matched: [],
  redirectedFrom: undefined
};

config.global.mocks.$router = mockRouter;
config.global.mocks.$route = mockRoute;

// 提供全局组合式API模拟
config.global.provide = {
  router: mockRouter,
  route: mockRoute
};

// 模拟 localStorage
Object.defineProperty(window, 'localStorage', {
  value: {
    getItem: vi.fn(() => null),
    setItem: vi.fn(() => null),
    removeItem: vi.fn(() => null),
    clear: vi.fn(() => null)
  },
  writable: true
});

// 模拟 sessionStorage
Object.defineProperty(window, 'sessionStorage', {
  value: {
    getItem: vi.fn(() => null),
    setItem: vi.fn(() => null),
    removeItem: vi.fn(() => null),
    clear: vi.fn(() => null)
  },
  writable: true
});

// 模拟 fetch API
global.fetch = vi.fn(() =>
  Promise.resolve({
    ok: true,
    status: 200,
    json: () => Promise.resolve({
      Code: 1000,
      Data: [],
      Message: 'Success'
    })
  })
);

// 模拟 IntersectionObserver
global.IntersectionObserver = vi.fn(() => ({
  observe: vi.fn(),
  disconnect: vi.fn(),
  unobserve: vi.fn()
}));

// 模拟 ResizeObserver
global.ResizeObserver = vi.fn(() => ({
  observe: vi.fn(),
  disconnect: vi.fn(),
  unobserve: vi.fn()
}));

// 模拟 matchMedia
Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: vi.fn().mockImplementation(query => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: vi.fn(), // deprecated
    removeListener: vi.fn(), // deprecated
    addEventListener: vi.fn(),
    removeEventListener: vi.fn(),
    dispatchEvent: vi.fn()
  }))
});

// 模拟 window.open
global.open = vi.fn();

// 模拟 console 方法以避免测试输出噪音
global.console = {
  ...console,
  warn: vi.fn(),
  error: vi.fn(),
  log: vi.fn()
};

// 设置测试环境变量
process.env.NODE_ENV = 'test';
process.env.VITE_APP_ENV = 'test';
process.env.VITE_API_BASE_URL = 'http://localhost:3000';

// 清理函数 - 在每个测试后运行
afterEach(() => {
  vi.clearAllMocks();
  vi.clearAllTimers();
});
